package tec.stan.ling.core.notifier.impl;

import java.util.LinkedList;
import java.util.List;

import tec.stan.ling.core.notifier.Adapter;
import tec.stan.ling.core.notifier.Notification;
import tec.stan.ling.core.notifier.Notifier;

/**
 * An extensible notifier implementation.
 * 
 * @author Stan 张新潮
 * @date Jan 13, 2010
 */
public class NotifierImpl implements Notifier {
	/**
	 * The list of {@link tec.stan.ling.core.notifier.Adapter}s associated with
	 * the notifier.
	 */
	private List<Adapter> adapters;

	private boolean deliver = true;

	@Override
	public List<Adapter> getAdapters() {
		if (adapters == null) {
			adapters = new LinkedList<Adapter>();
		}
		return adapters;
	}

	@Override
	public void notify(Notification notification) {
		if (adapters != null && isDeliver()) {
			//copy the list because adapters may modify when looping 
			Adapter[] list = adapters.toArray(new Adapter[adapters.size()]);
			for(Adapter adapter:list){
				adapter.notifyChanged(notification);
			}
		}
	}

	@Override
	public boolean isDeliver() {
		return deliver;
	}

	@Override
	public void setDeliver(boolean deliver) {
		this.deliver = deliver;
	}

}
